#include "PlSDLSimpleNet.h"
#include "simpleudplua.h"
#include "../core/LuaUtils.h"


void SimpleUdpLua::pushStaticFuncs(lua_State* L)
{
	static const struct luaL_Reg mylib [] = {
		{"OpenConnection", li_openConnection},
		{"AllowIncomingConnections", li_allowIncomingConnections},
		{"ForbidIncomingConnections", li_forbidIncomingConnections},
		{"CloseConnection", li_closeConnection},
		{"SendMessage", li_sendMessage},
		{"AddMessageHandler", li_addMessageHandler},
		{"RemoveMessageHandler", li_removeMessageHandler},
		{"GetConnName", li_getConnName},
		{"SetConnName", li_setConnName},
		{"InitNetworking", li_initNetworking},
		{NULL, NULL}  /* sentinel */
	};

	// dump them into said table
	luaL_register(L, NULL, mylib);
}

int SimpleUdpLua::li_openConnection(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = lua_tostring(L, 2);
	int a2 = (int)lua_tointeger(L, 3);
	int a3 = (int)lua_tointeger(L, 4);
	int ret = op->openConnection(a1, a2, a3);
	lua_pushinteger(L, ret);
	return 1;
}


int SimpleUdpLua::li_allowIncomingConnections(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	bool ret = op->allowIncomingConnections(a1);
	lua_pushboolean(L, ret);
	return 1;
}


int SimpleUdpLua::li_forbidIncomingConnections(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	bool ret = op->forbidIncomingConnections(a1);
	lua_pushboolean(L, ret);
	return 1;
}


int SimpleUdpLua::li_closeConnection(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	op->closeConnection(a1);
	
	return 0;
}


int SimpleUdpLua::li_sendMessage(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	std::string a2 = lua_tostring(L, 3);
	IData a3 = popIData(L, 4);
	bool a4 = (lua_toboolean(L, 5) > 0);
	op->sendMessage(a1, a2, a3, a4);
	
	return 0;
}


int SimpleUdpLua::li_addMessageHandler(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = lua_tostring(L, 2);
	AnyAOPtr a2 = unboxAOPtr(L, 3);
	op->addMessageHandler(a1, a2);
	
	return 0;
}


int SimpleUdpLua::li_removeMessageHandler(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	AnyAOPtr a1 = unboxAOPtr(L, 2);
	op->removeMessageHandler(a1);
	
	return 0;
}


int SimpleUdpLua::li_getConnName(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	std::string ret = op->getConnName(a1);
	lua_pushlstring(L, ret.data(), ret.length());
	return 1;
}


int SimpleUdpLua::li_setConnName(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	std::string a2 = lua_tostring(L, 3);
	op->setConnName(a1, a2);
	
	return 0;
}


int SimpleUdpLua::li_initNetworking(lua_State* L)
{
	SimpleUDPConnection* op = (SimpleUDPConnection*)typeCheckUnboxAO(L,1,"SimpleUDPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	op->initNetworking();
	
	return 0;
}


